home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFtoSpr - Star Fighter 3000 graphics converter
- * Quit confirm dbox
- * Copyright (C) 2000 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdio.h>
-
- /* RISC OS library files */
- #include "event.h"
- #include "toolbox.h"
- #include "quit.h"
- #include "wimplib.h"
- #include "wimp.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "RoundRobin.h"
- #include "TboxBugs.h"
- #ifdef FOCUS_CRAP
- #include "InputFocus.h"
- #endif
- #include "ViewsMenu.h"
-
- /* Local headers */
- #include "PreQuit.h"
- #include "Main.h"
-
- static ObjectId dbox_id = NULL_ObjectId;
- static int quit_sender;
- #ifdef HELP_CRAP
- static int window_handle;
- #endif
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _PreQuit_event_handler;
- #ifdef HELP_CRAP
- static WimpMessageHandler _PreQuit_menus_deleted;
- #endif
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void PreQuit_initialise(ObjectId PreQuit_id)
- {
- /* Record ID */
- dbox_id = PreQuit_id;
-
- /* Install handlers */
- #ifdef FOCUS_CRAP
- EF(event_register_toolbox_handler(PreQuit_id, Quit_AboutToBeShown, InputFocus_recordcaretpos, NULL));
- #endif
- EF(event_register_toolbox_handler(PreQuit_id, -1, _PreQuit_event_handler, NULL)); /* all events */
-
- #ifdef HELP_CRAP
- ObjectId window_id;
- EF(quit_get_window_id(0, PreQuit_id, &window_id));
- EF(window_get_wimp_handle(0, window_id, &window_handle));
- EF(event_register_message_handler(Wimp_MMenusDeleted, _PreQuit_menus_deleted, NULL));
- /* (See TboxBugs.h for explanation of this. If undefining HELP_CRAP,
- don't forget to set object template to generate Quit_DialogueCompleted
- event) */
- #endif
- }
-
- /* ----------------------------------------------------------------------- */
-
- bool TRY_QUIT(int task_handle)
- {
- ObjectId object;
- int scanprogress_count = 0;
- char buffer[16];
-
- object = ViewMenu_getfirst();
- while(object != NULL_ObjectId) {
- /* Don't report error if object doesn't exist */
- if(toolbox_get_template_name(0, object, buffer, sizeof(buffer), NULL) == NULL) {
- if(strcmp(buffer, "Scan") == 0)
- scanprogress_count++;
- }
- object = ViewMenu_getnext(object);
- }
- if(scanprogress_count > 1) {
- sprintf(buffer, "%d", scanprogress_count);
- RE(quit_set_message(0, dbox_id, msgs_lookup_sub1("PlurUNS", buffer)))
- }
- else {
- if(scanprogress_count == 1)
- RE(quit_set_message(0, dbox_id, msgs_lookup("SingUNS")))
- else
- return true; /* Don't mind quitting */
- }
-
- quit_sender = task_handle;
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, dbox_id, Toolbox_ShowObject_Centre, NULL, NULL_ObjectId, NULL_ComponentId))
- return false; /* Don't want to quit */
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _PreQuit_event_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- switch(event_code) {
- case Quit_AboutToBeShown:
- RoundRobin_suspend(); /* freeze all directory scans */
- #ifdef FOCUS_CRAP
- return 0; /* pass event on (to InputFocus_recordcaretpos) */
- #else
- return 1; /* claim event */
- #endif
-
- case Quit_Quit:
- #ifdef FOCUS_CRAP
- /* We won't be alive to hear the MenusDeleted msg, so fake it */
- RE(InputFocus_restorecaret())
- #endif
- if(quit_sender != 0) {
- /* Restart desktop shutdown */
- WimpKeyPressedEvent key_event;
- RE(wimp_get_caret_position(&key_event.caret))
- key_event.key_code = 0x1FC;
- RE(wimp_send_message(Wimp_EKeyPressed, &key_event, quit_sender, 0, NULL))
-
- #ifdef QUIT_ON_SHUTDOWN
- /* Quit immediately */
- exit(EXIT_SUCCESS);
- #else
- /* Do as Paint, Edit and Draw do - that is, discard all data */
- ObjectId object = ViewMenu_getfirst();
- while(object != NULL_ObjectId) {
- /* Don't report error if object doesn't exist */
- char buffer[16];
- if(toolbox_get_template_name(0, object, buffer, sizeof(buffer), NULL) == NULL) {
- if(strcmp(buffer, "Scan") == 0)
- RE(toolbox_delete_object(0, object))
- }
- object = ViewMenu_getnext(object);
- }
- #endif
-
- } else {
- /* Quit application */
- exit(EXIT_SUCCESS);
- }
- return 1; /* claim event */
-
- #ifndef HELP_CRAP
- case Quit_DialogueCompleted:
- /* Resume all directory scans */
- RE(RoundRobin_resume())
- return 1; /* claim event */
- #endif
- }
- return 0; /* pass event on */
- }
-
- /* ----------------------------------------------------------------------- */
-
- #ifdef HELP_CRAP
- static int _PreQuit_menus_deleted(WimpMessage *message, void *handle)
- {
- /* 'Menu tree' has been closed - is menu block our wimp window? */
- if(message->data.words[0] == window_handle)
- RoundRobin_resume(); /* yes - resume all directory scans */
-
- return 0; /* pass the event on to other handlers */
- }
- #endif
-